package lzupdater.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.gui.actions.BrowseAction;
import lzupdater.gui.actions.UpdateAction;
import lzupdater.gui.elements.LocalizedJButton;
import lzupdater.gui.elements.LocalizedJLabel;
import lzupdater.gui.listeners.FocusLostTask;
import lzupdater.gui.listeners.LZCheckBoxListener;
import lzupdater.gui.listeners.LZFieldListener;
import lzupdater.gui.listeners.ProfileListener;
import lzupdater.log.Logger;
import lzupdater.utils.LZULogger;
import lzupdater.utils.LZUProfileManager;
import lzupdater.utils.Profile;
import lzupdater.utils.Profile.ProfilePropertyId;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 * @author Fraskor
 */
public class ProfilePanel extends HubComponent {

	private JPanel panel;

	private LocalizedJLabel profileNameLabel;
	private LocalizedJLabel loginLabel;
	private LocalizedJLabel passwordLabel;
	private LocalizedJLabel savePasswordLabel;
	private LocalizedJLabel urlLabel;
	private LocalizedJLabel fileLabel;

	private LocalizedJButton renameButton;
	private LocalizedJButton browserButton;
	private LocalizedJButton updateButton;
	private LocalizedJButton launchAndUpdateButton;

	private JTextField profileNameField;
	private JTextField loginField;
	private JPasswordField passwordField;
	private JTextField urlField;
	private JTextField fileField;
	private JCheckBox savePasswordCheckBox;

	private LZFieldListener renameListener;
	private LZFieldListener loginListener;
	private LZFieldListener passwordListener;
	private LZFieldListener urlListener;
	private LZFieldListener fileListener;
	private LZCheckBoxListener savePasswordAction;

	private LZFieldListener[] fieldListeners;
	private JTextField[] textFields;

	private UpdateAction updateAction;
	private UpdateAction launchAndUpdateAction;

	private LZULogger logger;
	private LZUProfileManager profileManager;

	public ProfilePanel(CommunicationHub hub) {
		super(hub);
		panel = new JPanel();
	}

	@Override
	public void initializeCommunication() {
		logger = hub.getComponent(LZULogger.class);
		profileManager = hub.getComponent(LZUProfileManager.class);
	}

	@Override
	public void initialize() {
		initialiseGUI(logger);
	}

	public void initialiseGUI(Logger logger) {

		profileNameLabel = new LocalizedJLabel("Profile name:");
		loginLabel = new LocalizedJLabel("Login:");
		passwordLabel = new LocalizedJLabel("Password:");
		savePasswordLabel = new LocalizedJLabel("Save password");
		urlLabel = new LocalizedJLabel("Roster URL:");
		fileLabel = new LocalizedJLabel("LZProfiler file:");

		renameButton = new LocalizedJButton("Rename");
		browserButton = new LocalizedJButton("Browse...");
		updateButton = new LocalizedJButton("Update");
		launchAndUpdateButton = new LocalizedJButton("Launch WoW & Update");

		profileNameField = new JTextField();
		loginField = new JTextField();
		passwordField = new JPasswordField();
		urlField = new JTextField();
		fileField = new JTextField();
		savePasswordCheckBox = new JCheckBox();

		initialiseActions(logger);

		GridBagLayout layout = new GridBagLayout();

		layout.rowHeights = new int[] { 27, 27, 27, 27, 27, 27 };

		panel.setLayout(layout);
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		c.weightx = 0.0;
		c.weighty = 0.0;

		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(0, 5, 0, 2);
		c.weightx = 0.0;
		c.weighty = 0.0;
		panel.add(profileNameLabel, c);

		c.gridx = 0;
		c.gridy = 1;
		c.insets = new Insets(0, 5, 0, 2);
		c.weightx = 0.0;
		c.weighty = 0.0;
		panel.add(loginLabel, c);

		c.gridx = 0;
		c.gridy = 2;
		c.insets = new Insets(0, 5, 0, 2);
		c.weightx = 0.0;
		c.weighty = 0.0;
		panel.add(passwordLabel, c);

		c.gridx = 0;
		c.gridy = 3;
		c.insets = new Insets(0, 5, 0, 2);
		c.weightx = 0.0;
		c.weighty = 0.0;
		panel.add(fileLabel, c);

		c.gridx = 0;
		c.gridy = 4;
		c.insets = new Insets(0, 5, 0, 2);
		c.weightx = 0.0;
		c.weighty = 0.0;
		panel.add(urlLabel, c);

		// Initialize the text fields

		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(0, 0, 0, 0);
		c.weightx = 1.0;
		c.weighty = 0.0;

		// ProfileName field
		c.gridx = 1;
		c.gridy = 0;
		panel.add(profileNameField, c);

		// Login field
		c.gridx = 1;
		c.gridy = 1;
		panel.add(loginField, c);

		// Password field
		c.gridx = 1;
		c.gridy = 2;
		panel.add(passwordField, c);

		c.gridx = 2;
		c.gridy = 2;
		c.gridwidth = 1;
		c.weightx = 0.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(createPasswordCheckBox(), c);

		// Roster File
		c.gridx = 1;
		c.gridy = 3;
		c.gridwidth = 1;
		c.weightx = 1.0;
		panel.add(fileField, c);

		// Roster URL
		c.gridx = 1;
		c.gridy = 4;
		c.gridwidth = 2;
		c.insets = new Insets(0, 0, 0, 5);
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(urlField, c);

		c.gridx = 2;
		c.gridy = 3;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.NONE;
		c.insets = new Insets(2, 10, 2, 10);
		c.weightx = 0.0;
		panel.add(browserButton, c);

		c.gridx = 2;
		c.gridy = 0;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.NONE;
		c.insets = new Insets(2, 10, 2, 10);
		c.weightx = 0.0;
		panel.add(renameButton, c);

		c.gridx = 1;
		c.gridy = 5;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.NONE;
		c.insets = new Insets(0, 0, 0, 0);
		updateButton.setSize(148, 27);
		panel.add(updateButton, c);

		c.gridx = 2;
		c.gridy = 5;
		c.gridwidth = 1;
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.NONE;
		c.insets = new Insets(0, 0, 0, 0);
		launchAndUpdateButton.setSize(148, 27);
		panel.add(launchAndUpdateButton, c);
	}

	public JPanel getGui() {
		return panel;
	}

	private JPanel createPasswordCheckBox() {
		JPanel jp = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		jp.setLayout(layout);
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		jp.add(savePasswordCheckBox, c);
		jp.add(savePasswordLabel, c);

		return jp;
	}

	private void initialiseActions(Logger logger) {
		// Setup Action

		renameListener = new LZFieldListener(profileNameField,
				ProfilePropertyId.NAME, true) {
			@Override
			protected void initialiseProperty(Profile profile,
					ProfilePropertyId propertyId, String value) {
				renameButton.setEnabled(profileManager.checkRenaming(value));
			}
		};

		loginListener    = new LZFieldListener(loginField, ProfilePropertyId.LOGIN);
		passwordListener = new LZFieldListener(passwordField, ProfilePropertyId.PASSWORD);
		urlListener      = new LZFieldListener(urlField, ProfilePropertyId.ROSTER_URL);
		fileListener     = new LZFieldListener(fileField, ProfilePropertyId.ROSTER_FILE);

		fieldListeners = new LZFieldListener[] { renameListener, loginListener,
				passwordListener, urlListener, fileListener };
		textFields = new JTextField[] { profileNameField, loginField,
				passwordField, urlField, fileField };

		updateAction = new UpdateAction(hub, updateButton, false);
		launchAndUpdateAction = new UpdateAction(hub, launchAndUpdateButton, true);
		
		updateAction.setLZFieldListener(fieldListeners);

		for (int i = 0; i < fieldListeners.length; i++) {
			textFields[i].addActionListener(fieldListeners[i]);
			textFields[i].getDocument().addDocumentListener(fieldListeners[i]);
		}

		profileNameField.addFocusListener(new FocusLostTask(500) {
			public void performTask(long time) {
				Profile p = profileManager.getCurrentProfile();
				if (p != null)
					profileNameField.setText(p.getName());
			}
		});

		savePasswordAction = new LZCheckBoxListener(savePasswordCheckBox,
				ProfilePropertyId.SAVE_PASSWORD);
		savePasswordCheckBox.addActionListener(savePasswordAction);

		BrowseAction browseAction = new BrowseAction(fileListener);

		renameButton.setAction(new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -6620702555532319418L;

			public void actionPerformed(ActionEvent e) {
				String newName = profileNameField.getText();
				profileManager.renameCurrentProfile(newName);
			}
		});

		browserButton.setAction(browseAction);
		updateButton.setAction(updateAction);
		launchAndUpdateButton.setAction(launchAndUpdateAction);

		profileManager.addProfileListener(new ProfileListener[] {
				renameListener, browseAction, browserButton, loginListener,
				passwordListener, urlListener, fileListener, updateAction,
				launchAndUpdateAction, savePasswordAction });

	}

	public void windowIsClosing() {
		for (LZFieldListener f : fieldListeners) {
			f.windowIsClosing();
		}
	}

}
